<?php
/**
 * name 刘杨
 */
namespace backend\models;

use Yii;
use yii\data\Pagination;
/**
 * This is the model class for table "admin_user".
 *
 * @property string $id
 * @property string $name
 * @property string $pwd
 * @property string $realname
 * @property string $phone
 * @property integer $addtime
 * @property integer $end_time
 * @property integer $status
 */
class User extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'admin_user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'pwd', 'realname', 'phone'], 'required'],
            [['addtime', 'end_time', 'status'], 'integer'],
            [['name', 'pwd', 'realname'], 'string', 'max' => 50],
            [['phone'], 'string', 'max' => 20]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '',
            'pwd' => '',
            'realname' => '',
            'phone' => '',
            'addtime' => '',
            'end_time' => '',
            'status' => '',
        ];
    }
    /*
    *@搜索用户
     */
    function sousuo($sou)
    {
        $data = $this::find()->where(['like','name',$sou])->asArray()->all();
        if(empty($data)) $count = $this::find()->count(); else $count = count($data);
        $pagination = new Pagination([
        'defaultPageSize' => 3,
        'totalCount' => $count,
        ]);
        if(empty($data))
        {
            $user = $this::find()->offset($pagination->offset)
            ->limit($pagination->limit)
            ->orderBy('id desc')
            ->asArray()
            ->all();
        }else{
            $user = $this::find()->offset($pagination->offset)
            ->limit($pagination->limit)
            ->orderBy('id desc')
            ->where(['like','name',$sou])
            ->asArray()
            ->all();
            foreach ($user as $k => $v) {
                $user[$k]['name'] = str_replace($sou,"<font color='red'>$sou</font>",$user[$k]['name']);
            }
        }
        return array('data'=>$user,'pagination'=>$pagination);
    }
}
